package com.t4.courseware.controller;


import com.alibaba.fastjson.JSONObject;
import com.t4.courseware.common.Result;
import com.t4.courseware.handler.NoAuth;
import com.t4.courseware.pojo.Order;
import com.t4.courseware.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public Result createOrder(@RequestBody Order order) {

        return orderService.createOrder(order);
    }

    @NoAuth
    @PostMapping("/callback")
    public String asyncCallBack(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        JSONObject jsonObject = new JSONObject();
        for (String key : parameterMap.keySet()) {
            String[] strings = parameterMap.get(key);
            jsonObject.put(key, strings[0]);
        }
        log.info(jsonObject.toJSONString());
        boolean b = orderService.callBack(jsonObject, request);
        return b ? "SUCCESS" : null;
    }


}